<?php // 4.0
class quote {
  function loaddata() {
    global $masterclass;

    $masterclass->loadmodule('invoices');
    $masterclass->loadmodule('customers');
    $masterclass->loadmodule('personnel');
    $masterclass->loadmodule('companies');

    $masterclass->websiteconfig['title']='New Invoice - Invoice Info';
    
    if (!isset($masterclass->sitevars['invoicesid']) || $masterclass->sitevars['invoicesid']=='') {
      $masterclass->sitevars['invoices_documentid']=(ABS($masterclass->allclasses['invoices']->fetch_invoices_lastdocid())+1);  
        
      if ($masterclass->sitevars['customerid']>0) {
        $strwhere=sprintf(" `id`=%d ",$masterclass->allclasses['mysql_functions']->formatmysqldbl($masterclass->sitevars['customerid']));
        $customerinfo=$masterclass->fetch_records('customers',1,$strwhere);

        if (!empty($customerinfo)) {
          $masterclass->sitevars['invoices_recipientlineone']=$customerinfo[0]['first'].' '.$customerinfo[0]['last'];
          $masterclass->sitevars['invoices_recipientlinetwo']=$customerinfo[0]['address'];
          $masterclass->sitevars['invoices_recipientlinethree']=$customerinfo[0]['city'].', '.$customerinfo[0]['state'].'  '.$customerinfo[0]['zip'];
          $masterclass->sitevars['invoices_recipientlinefour']=$customerinfo[0]['emaila'];
          $masterclass->sitevars['invoices_recipientlinefive']=$customerinfo[0]['dayphone'];
        }
      }
    }
    $masterclass->site['postfields'][] = array(
      'table'=>'invoices_documents',
      'trackingid'=>'invoicesid',
      'fields'=>array(
        'documentid'=>'invoices_documentid',
        
        'net'=>'invoices_net',

        'recipientlineone'=>'invoices_recipientlineone',
        'recipientlinetwo'=>'invoices_recipientlinetwo',
        'recipientlinethree'=>'invoices_recipientlinethree',
        'recipientlinefour'=>'invoices_recipientlinefour',
        'recipientlinefive'=>'invoices_recipientlinefive',
        
        'documentdate'=>'invoices_datehidden',
        'duedate'=>'invoices_duehidden'
      )
    );

  return;
  }
  
  function savedata() {
    global $masterclass;


    if (!isset($masterclass->sitevars['invoicesid']) || $masterclass->sitevars['invoicesid']=='') {
      if ($masterclass->sitevars['customerid']>0) {
        $strwhere=sprintf(" `id`=%d ",$masterclass->allclasses['mysql_functions']->formatmysqldbl($masterclass->sitevars['customerid']));
        $customerinfo=$masterclass->fetch_records('customers',1,$strwhere);

        if (!empty($customerinfo)) {
          $invoices_recipientlineone=$customerinfo[0]['first'].' '.$customerinfo[0]['last'];
          $invoices_recipientlinetwo=$customerinfo[0]['address'];
          $invoices_recipientlinethree=$customerinfo[0]['city'].', '.$customerinfo[0]['state'].'  '.$customerinfo[0]['zip'];
          $invoices_recipientlinefour=$customerinfo[0]['emaila'];
          $invoices_recipientlinefive=$customerinfo[0]['dayphone'];
        }
      }
      $companyinfo=$masterclass->allclasses['companies']->fetch_companyinfo();
      if (!empty($companyinfo)) {
        $invoices_companylineone=$companyinfo[0]['name'];
        $invoices_companylinetwo=$companyinfo[0]['address'];
        $invoices_companylinethree=$companyinfo[0]['city'].', '.$companyinfo[0]['state'].' '.$companyinfo[0]['zip'];
        $invoices_companylinefour=$companyinfo[0]['phone'];
        $invoices_companylinefive=$companyinfo[0]['www'];
      }

      $masterclass->sitevars['invoices_documentdate']=date("Y/m/d");
      $masterclass->sitevars['invoices_duedate']=date("Y/m/d");

      $lastdocid=$masterclass->allclasses['invoices']->fetch_invoices_lastdocid();
      $newdocid=$lastdocid+1;
      if ($lastdocid==$newdocid) { $newdocid=0; }


      $dbinfo = array(
        'table'=>'invoices_documents',
        'trackingid'=>'invoicesid',
        'fields'=>array(
          'customer'=>'customerid',
          'documentdate'=>'invoices_documentdate',
          'duedate'=>'invoices_duedate'
        ),
        'static'=>array(
          'active'=>'1',
          'personnel'=>$masterclass->site['personnelid'],
          'documentid'=>$newdocid,
          'documenttype'=>'quote',
          'net'=>'0',
          'recipientlineone'=>$invoices_recipientlineone,
          'recipientlinetwo'=>$invoices_recipientlinetwo,
          'recipientlinethree'=>$invoices_recipientlinethree,
          'recipientlinefour'=>$invoices_recipientlinefour,
          'recipientlinefive'=>$invoices_recipientlinefive,
          'companylineone'=>$invoices_companylineone,
          'companylinetwo'=>$invoices_companylinetwo,
          'companylinethree'=>$invoices_companylinethree,
          'companylinefour'=>$invoices_companylinefour,
          'companylinefive'=>$invoices_companylinefive
        )
      );

      $masterclass->sitevars['invoicesid']=$masterclass->db_insert($dbinfo);
    }
    
    $masterclass->savedata();
    $masterclass->site['forward']=$masterclass->mainconfig['dir'].$masterclass->mainconfig['htmldirectory'].'invoices/quote_add.php?'.$masterclass->trackingvar_query();

  return;
  }

  function display() {
    global $masterclass;

    $arrdocumentdates=$masterclass->allclasses['invoices']->fetch_invoicesdocumentdates($masterclass->sitevars['invoicesid']);
    $masterclass->sitevars['invoices_documentdate']=$arrdocumentdates[0];
    $masterclass->sitevars['invoices_duedate']=$arrdocumentdates[1];
    $masterclass->sitevars['invoices_paid']=$arrdocumentdates[2];
    
    if ($masterclass->sitevars['invoices_documentdate']=='0000-00-00' || $masterclass->sitevars['invoices_documentdate']=='') { $masterclass->sitevars['invoices_documentdate']=date("Y/m/d"); } else { $masterclass->sitevars['invoices_documentdate']=date("Y/m/d",strtotime($masterclass->sitevars['invoices_documentdate'])); }
    if ($masterclass->sitevars['invoices_duedate']=='0000-00-00' || $masterclass->sitevars['invoices_duedate']=='') { $masterclass->sitevars['invoices_duedate']=''; } else { $masterclass->sitevars['invoices_duedate']=date("Y/m/d",strtotime($masterclass->sitevars['invoices_duedate'])); }
    if ($masterclass->sitevars['invoices_paid']=='0000-00-00' || $masterclass->sitevars['invoices_paid']=='') { $masterclass->sitevars['invoices_paid']=''; } else { $masterclass->sitevars['invoices_paid']=date("Y/m/d",strtotime($masterclass->sitevars['invoices_paid'])); }

    if ($masterclass->sitevars['invoices_duedate']=='' || $masterclass->sitevars['invoices_duedate']=='0000-00-00') {
      $masterclass->sitevars['invoices_duedate']=$masterclass->sitevars['invoices_documentdate'];
    }

    echo '<a href="'.$masterclass->mainconfig['dir'].$masterclass->mainconfig['htmldirectory'].'">Home</a> | <a href="'.$masterclass->mainconfig['dir'].$masterclass->mainconfig['htmldirectory'].'invoices/">Invoices</a> | <a href="'.$masterclass->mainconfig['dir'].$masterclass->mainconfig['htmldirectory'].'customers/">Customers</a><hr>';

    echo $masterclass->allclasses['websites']->sitebox_headline('Invoice Information','');
    echo 'Doc #:&nbsp;'.$masterclass->allclasses['websites']->disp_inputtext('invoices_documentid','','5','11').'&nbsp;Last: '.$masterclass->allclasses['invoices']->fetch_invoices_lastdocid().'<br>';
    echo 'Date:&nbsp;'.$masterclass->allclasses['websites']->disp_inputtext('invoices_documentdate','','11','10').'<br>';
    echo 'Terms:&nbsp;';
    $options=array();
    $options[]=array('0','Upon Receipt');
    $options[]=array('5','Net 5');
    $options[]=array('10','Net 10');
    $options[]=array('15','Net 15');
    $options[]=array('30','Net 30');
    $options[]=array('45','Net 45');
    $options[]=array('90','Net 90');
    echo $masterclass->allclasses['websites']->disp_select($options,'invoices_net',$masterclass->sitevars['invoices_net'],'65%');
    echo '<br>';

    echo '<b>Recipient Information:</b><br>';
    echo $masterclass->allclasses['websites']->disp_inputtext('invoices_recipientlineone').'<br>';
    echo $masterclass->allclasses['websites']->disp_inputtext('invoices_recipientlinetwo').'<br>';
    echo $masterclass->allclasses['websites']->disp_inputtext('invoices_recipientlinethree').'<br>';
    echo $masterclass->allclasses['websites']->disp_inputtext('invoices_recipientlinefour').'<br>';
    echo $masterclass->allclasses['websites']->disp_inputtext('invoices_recipientlinefive').'<br>';

    echo '<input type="submit" value="Add Items">';

  return;
  }
}
?>